import random


class Address:
    def __init__(self ,country ,province ,street ,houseNumber):
        self.contry =country
        self.province =province
        self.street =street
        self.houseNumber =houseNumber

    def __str__(self):
        return ''.join([self.contry,self.province,self.street,self.houseNumber])

class User(object):
    def __init__(self ,name ,password ,address ,money ,bankName):
        self.userStr = ''
        self.name =name
        self.password = password
        self.address =address
        self.money = money
        self.bankName =bankName


class Bank(object):
    def __init__(self ,name):
        self.name = name
        self.users = {}
        self.userNum = 0
        self.userStrs = []
        users = [
        User('张三' ,'123456' ,Address('中国' ,'河南' ,'东风路' ,'十六号') ,2000 ,'中国工商银行')
        ,User('李四' ,'123456' ,Address('中国' ,'河南' ,'东风路' ,'十七号') ,2000 ,'中国工商银行')
        ,User('王五' ,'123456' ,Address('中国' ,'河南' ,'东风路' ,'十八号') ,2000 ,'中国工商银行')
        ,User('赵六' ,'123456' ,Address('中国' ,'河南' ,'东风路' ,'十九号') ,2000 ,'中国工商银行')
        ,User('田七' ,'123456' ,Address('中国' ,'河南' ,'东风路' ,'二十号') ,2000 ,'中国工商银行') ]
        for user in users:
            self.addUser(user)


    def addUser(self ,user):
        if self.users.get(user ,'no') != 'no':
            return 2
        elif self.userNum >= 100:
            return 3
        else:
            userName = []
            for i in range(0 ,8 ,1):
                userName.append(str(random.randint(0,9)))
            userStr = ''.join(userName)
            if userStr in self.userStrs:
                self.addUser(user)
            else:
                self.users[user] = userStr
                self.userStrs.append(userStr)
                print('您的账号为：' ,userStr)
                return 1

    def addMoney(self ,userStr ,amount ,password = None):
        if userStr in self.userStrs:
            for k,v in self.users.items():
                if v == userStr:
                    if password == None or k.password == password:
                        if k.money + amount > 0:
                            k.money = k.money + amount
                            print('您当前的余额为' ,k.money)
                            return 0
                        else:
                            return 3
                    else:
                        return 2
        else:
            return 1

    def reduceMoney(self ,userStr ,password ,amount):
        result = self.addMoney(userStr ,-amount ,password)
        return result

    def moveMoney(self ,userStrOut ,userStrIn ,outPassword ,amount):
        reduceResult = self.reduceMoney(userStrOut ,outPassword ,amount)
        if reduceResult == 0:
            addResult = self.addMoney(userStrIn ,amount)
            if addResult == False:
                self.addMoney(userStrOut ,amount)
                return 1
            else:
                return 0
        return reduceResult

    def printMoney(self ,userStr ,password):
        if self.reduceMoney(userStr ,password ,0) == 0:
            for k,v in self.users.items():
                if v == userStr:
                    print('用户账号为     ：' ,v)
                    print('余额为        ：' ,k.money)
                    print('用户现居住地   ：' ,k.address)
                    print('当前账户的开户行：' ,self.name)
        else:
            return 1


    def printMenu(self):
        print('************************************')
        print('*          ',self.name,'             *')
        print('*          账户管理系统              *')
        print('*             v 1.0                *')
        print('************************************')
        print('')
        print('*1.开户                             *')
        print('*2.存钱                             *')
        print('*3.取钱                             *')
        print('*4.转账                             *')
        print('*5.查询                             *')
        print('*6.Bey！                            *')

    def collectUserMessage(self):
        name = input('请输入您的姓名')
        password = input('请输入你想要设置的密码')
        country = input('请输入您所在的国家')
        province = input('请输入你所在的省')
        street = input('请输入你所在的街道')
        houseNumber = input('请输入你所在的门牌号')
        money = 0
        bankName = self.name
        return User(name ,password ,Address(country ,province ,street ,houseNumber) ,money ,bankName)

    def mainRun(self):
        while True:
            result = -1
            self.printMenu()
            choice = input('请输入你的选择')
            if choice == '1':
                user = self.collectUserMessage()
                result = self.addUser(user)
                if result == 1:
                    print('添加成功')
                elif result == 2:
                    print('用户已存在')
                elif result == 3:
                    print('系统已经注册满')
                continue
            elif choice == '2':
                amount = int(input('请输入你想要存的金额'))
                userStr = input('请输入你的账户')
                result = self.addMoney(userStr ,amount)
            elif choice == '3':
                amount = int(input('请输入你想要取出的金额'))
                userStr = input('请输入你的账户')
                password = input('请输入你的账户密码')
                result = self.reduceMoney(userStr ,password ,amount)
            elif choice == '4':
                amount = int(input('请输入你想要转账的金额'))
                userStrOut = input('请输入你的账户')
                password = input('请输入你的账户密码')
                userStrIn = input('请输入你想要转账的账户')
                result = self.moveMoney(userStrOut ,userStrIn ,password ,amount)
            elif choice == '5':
                userStr = input('请输入你的账户')
                password = input('请输入你的账户密码')
                result = self.printMoney(userStr ,password)
            elif choice == '6':
                break
            else:
                print('您的输入有误，请重新输入')
            if result == 1:
                print('账号不存在')
            elif result == 2:
                print('密码不正确')
            elif result == 3:
                print('钱不够')
            elif result == 0:
                print('操作成功')

bank = Bank('中国工商银行')
print(bank.userStrs)
bank.mainRun()

